-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Enforce tail call type is related to body return type in borrowck #144917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce tail call type is related to body return type in borrowck #144917
Conversation
r? @WaffleLapkin though lcnr can review it too if they want, this change should be pretty straightforward. |
|
} else { | ||
ConstraintCategory::UseAsConst | ||
} | ||
if is_diverging { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really care about the ordering here but the true
branch seemed easier to put first
4504cdb
to
a573fd9
Compare
@bors r+ rollup |
…ifetimes, r=lcnr Enforce tail call type is related to body return type in borrowck Like all call terminators, tail call terminators instantiate the binder of the callee signature with region variables and equate the arg operand types with that signature's args to ensure that the call is valid. However, unlike normal call terminators, we were forgetting to also relate the return type of the call terminator to anything. In the case of tail call terminators, the correct thing is to relate it to the return type of the caller function (or in other words, the return local `_0`). This meant that if the caller's return type had some lifetime constraint, then that constraint wouldn't flow through the signature and affect the args. This is what's happening in the example test I committed: ```rust fn link(x: &str) -> &'static str { become passthrough(x); } fn passthrough<T>(t: T) -> T { t } fn main() { let x = String::from("hello, world"); let s = link(&x); drop(x); println!("{s}"); } ``` Specifically, the type `x` is `'?0 str`, where `'?0` is some *universal* arg. The type of `passthrough` is `fn(&'?1 str) -> &'?1 str`. Equating the args sets `'?0 = '?1`. However, we need to also equate the return type `&'?1 str` to `&'static str` so that we eventually require that `'?0 = 'static`, which is a borrowck error! ----- Look at the first commit for the functional change, and the second commit is just a refactor because we don't need to pass `Option<BasicBlock>` to `check_call_dest`, but just whether or not the terminator is expected to be diverging (i.e. if the return type is `!`). Fixes rust-lang#144916
…ifetimes, r=lcnr Enforce tail call type is related to body return type in borrowck Like all call terminators, tail call terminators instantiate the binder of the callee signature with region variables and equate the arg operand types with that signature's args to ensure that the call is valid. However, unlike normal call terminators, we were forgetting to also relate the return type of the call terminator to anything. In the case of tail call terminators, the correct thing is to relate it to the return type of the caller function (or in other words, the return local `_0`). This meant that if the caller's return type had some lifetime constraint, then that constraint wouldn't flow through the signature and affect the args. This is what's happening in the example test I committed: ```rust fn link(x: &str) -> &'static str { become passthrough(x); } fn passthrough<T>(t: T) -> T { t } fn main() { let x = String::from("hello, world"); let s = link(&x); drop(x); println!("{s}"); } ``` Specifically, the type `x` is `'?0 str`, where `'?0` is some *universal* arg. The type of `passthrough` is `fn(&'?1 str) -> &'?1 str`. Equating the args sets `'?0 = '?1`. However, we need to also equate the return type `&'?1 str` to `&'static str` so that we eventually require that `'?0 = 'static`, which is a borrowck error! ----- Look at the first commit for the functional change, and the second commit is just a refactor because we don't need to pass `Option<BasicBlock>` to `check_call_dest`, but just whether or not the terminator is expected to be diverging (i.e. if the return type is `!`). Fixes rust-lang#144916
Rollup of 11 pull requests Successful merges: - #143857 (Port #[macro_export] to the new attribute parsing infrastructure) - #144133 (Stabilize const TypeId::of) - #144676 (Add documentation for unstable_feature_bound) - #144682 (Stabilize `strict_overflow_ops`) - #144835 (Anonymize binders in tail call sig) - #144836 (Change visibility of Args new function) - #144900 (Stabilize `unsigned_signed_diff` feature) - #144917 (Enforce tail call type is related to body return type in borrowck) - #144926 (Correct the use of `must_use` on btree::IterMut) - #144928 (Drop `rust-version` from `rustc_thread_pool`) - #144945 (Autolabel PRs that change explicit tail call tests as `F-explicit_tail_calls`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 11 pull requests Successful merges: - #143857 (Port #[macro_export] to the new attribute parsing infrastructure) - #144133 (Stabilize const TypeId::of) - #144676 (Add documentation for unstable_feature_bound) - #144682 (Stabilize `strict_overflow_ops`) - #144835 (Anonymize binders in tail call sig) - #144836 (Change visibility of Args new function) - #144900 (Stabilize `unsigned_signed_diff` feature) - #144917 (Enforce tail call type is related to body return type in borrowck) - #144926 (Correct the use of `must_use` on btree::IterMut) - #144928 (Drop `rust-version` from `rustc_thread_pool`) - #144945 (Autolabel PRs that change explicit tail call tests as `F-explicit_tail_calls`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 11 pull requests Successful merges: - #143857 (Port #[macro_export] to the new attribute parsing infrastructure) - #144133 (Stabilize const TypeId::of) - #144676 (Add documentation for unstable_feature_bound) - #144682 (Stabilize `strict_overflow_ops`) - #144835 (Anonymize binders in tail call sig) - #144836 (Change visibility of Args new function) - #144900 (Stabilize `unsigned_signed_diff` feature) - #144917 (Enforce tail call type is related to body return type in borrowck) - #144926 (Correct the use of `must_use` on btree::IterMut) - #144928 (Drop `rust-version` from `rustc_thread_pool`) - #144945 (Autolabel PRs that change explicit tail call tests as `F-explicit_tail_calls`) r? `@ghost` `@rustbot` modify labels: rollup
…ifetimes, r=lcnr Enforce tail call type is related to body return type in borrowck Like all call terminators, tail call terminators instantiate the binder of the callee signature with region variables and equate the arg operand types with that signature's args to ensure that the call is valid. However, unlike normal call terminators, we were forgetting to also relate the return type of the call terminator to anything. In the case of tail call terminators, the correct thing is to relate it to the return type of the caller function (or in other words, the return local `_0`). This meant that if the caller's return type had some lifetime constraint, then that constraint wouldn't flow through the signature and affect the args. This is what's happening in the example test I committed: ```rust fn link(x: &str) -> &'static str { become passthrough(x); } fn passthrough<T>(t: T) -> T { t } fn main() { let x = String::from("hello, world"); let s = link(&x); drop(x); println!("{s}"); } ``` Specifically, the type `x` is `'?0 str`, where `'?0` is some *universal* arg. The type of `passthrough` is `fn(&'?1 str) -> &'?1 str`. Equating the args sets `'?0 = '?1`. However, we need to also equate the return type `&'?1 str` to `&'static str` so that we eventually require that `'?0 = 'static`, which is a borrowck error! ----- Look at the first commit for the functional change, and the second commit is just a refactor because we don't need to pass `Option<BasicBlock>` to `check_call_dest`, but just whether or not the terminator is expected to be diverging (i.e. if the return type is `!`). Fixes rust-lang#144916
Rollup of 21 pull requests Successful merges: - #137831 (Tweak auto trait errors) - #138689 (add nvptx_target_feature) - #140267 (implement continue_ok and break_ok for ControlFlow) - #143679 (Preserve the .debug_gdb_scripts section) - #143857 (Port #[macro_export] to the new attribute parsing infrastructure) - #143929 (Mark all deprecation lints in name resolution as deny-by-default and report-in-deps) - #144133 (Stabilize const TypeId::of) - #144369 (Upgrade semicolon_in_expressions_from_macros from warn to deny) - #144473 (Address libunwind.a inconsistency issues in the bootstrap program) - #144498 (Add --print target-spec-json-schema) - #144552 (Rehome 33 `tests/ui/issues/` tests to other subdirectories under `tests/ui/`) - #144659 (bootstrap: refactor mingw dist and fix gnullvm) - #144676 (Add documentation for unstable_feature_bound) - #144794 (Port `#[coroutine]` to the new attribute system) - #144835 (Anonymize binders in tail call sig) - #144836 (Change visibility of Args new function) - #144861 (Stabilize `panic_payload_as_str` feature) - #144910 (Add regression tests for seemingly fixed issues) - #144913 ([rustdoc] Fix wrong `i` tooltip icon) - #144917 (Enforce tail call type is related to body return type in borrowck) - #144924 (compiletest: add hint for when a ui test produces no errors) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 15 pull requests Successful merges: - #144195 (Parser: Recover from attributes applied to types and generic args) - #144794 (Port `#[coroutine]` to the new attribute system) - #144835 (Anonymize binders in tail call sig) - #144861 (Stabilize `panic_payload_as_str` feature) - #144917 (Enforce tail call type is related to body return type in borrowck) - #144948 (we only merge candidates for trait and normalizes-to goals) - #144956 (Gate const trait syntax) - #144970 (rustdoc: fix caching of intra-doc links on reexports) - #144972 (add code example showing that file_prefix treats dotfiles as the name of a file, not an extension) - #144975 (`File::set_times`: Update documentation and example to support setting timestamps on directories) - #144977 (Fortify generic param default checks) - #144996 (simplifycfg: Mark as changed when start is modified in collapse goto chain) - #144998 (mir: Do not modify NonUse in `super_projection_elem`) - #145000 (Remove unneeded `stage` parameter when setting up stdlib Cargo) - #145008 (Fix rustdoc scrape examples crash) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #144917 - compiler-errors:tail-call-linked-lifetimes, r=lcnr Enforce tail call type is related to body return type in borrowck Like all call terminators, tail call terminators instantiate the binder of the callee signature with region variables and equate the arg operand types with that signature's args to ensure that the call is valid. However, unlike normal call terminators, we were forgetting to also relate the return type of the call terminator to anything. In the case of tail call terminators, the correct thing is to relate it to the return type of the caller function (or in other words, the return local `_0`). This meant that if the caller's return type had some lifetime constraint, then that constraint wouldn't flow through the signature and affect the args. This is what's happening in the example test I committed: ```rust fn link(x: &str) -> &'static str { become passthrough(x); } fn passthrough<T>(t: T) -> T { t } fn main() { let x = String::from("hello, world"); let s = link(&x); drop(x); println!("{s}"); } ``` Specifically, the type `x` is `'?0 str`, where `'?0` is some *universal* arg. The type of `passthrough` is `fn(&'?1 str) -> &'?1 str`. Equating the args sets `'?0 = '?1`. However, we need to also equate the return type `&'?1 str` to `&'static str` so that we eventually require that `'?0 = 'static`, which is a borrowck error! ----- Look at the first commit for the functional change, and the second commit is just a refactor because we don't need to pass `Option<BasicBlock>` to `check_call_dest`, but just whether or not the terminator is expected to be diverging (i.e. if the return type is `!`). Fixes #144916
Like all call terminators, tail call terminators instantiate the binder of the callee signature with region variables and equate the arg operand types with that signature's args to ensure that the call is valid.
However, unlike normal call terminators, we were forgetting to also relate the return type of the call terminator to anything. In the case of tail call terminators, the correct thing is to relate it to the return type of the caller function (or in other words, the return local
_0
).This meant that if the caller's return type had some lifetime constraint, then that constraint wouldn't flow through the signature and affect the args.
This is what's happening in the example test I committed:
Specifically, the type
x
is'?0 str
, where'?0
is some universal arg. The type ofpassthrough
isfn(&'?1 str) -> &'?1 str
. Equating the args sets'?0 = '?1
. However, we need to also equate the return type&'?1 str
to&'static str
so that we eventually require that'?0 = 'static
, which is a borrowck error!Look at the first commit for the functional change, and the second commit is just a refactor because we don't need to pass
Option<BasicBlock>
tocheck_call_dest
, but just whether or not the terminator is expected to be diverging (i.e. if the return type is!
).Fixes #144916